<template>
  <div>
    <!-- 内容主体区域 -->
    <dtDialog :title="title" :visible.sync="visible" :is-loading="loading" @closeDialog="handleClose()" @comfirmBtn="onSubmit('form')">
      <el-form ref="form" slot="content" :rules="rules" :model="formData" label-width="82px">
        <!-- <el-form-item label="角色类型" prop="roleTypeCode">
          <el-select v-model.trim="formData.roleTypeCode" placeholder="请选择角色类型" style="width: 100%;">
            <el-option v-for="item in roleTypeList" :key="item.dictCode" :label="item.dictName" :value="item.dictCode" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model.trim="formData.roleName" maxlength="30" autocomplete="off" placeholder="请输入角色名称" show-word-limit class="limit" />
        </el-form-item>
        <el-form-item label="角色编码" prop="roleCode">
          <el-input v-model.trim="formData.roleCode" maxlength="30" autocomplete="off" placeholder="请输入角色编码" show-word-limit class="limit" />
        </el-form-item>
        <el-form-item label="排序" prop="roleSort">
          <el-input v-model.trim="formData.roleSort" :oninput="initPositiveInteger()" placeholder="请输入排序" maxlength="8" show-word-limit class="limit" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="formData.remark" maxlength="100" show-word-limit type="textarea" autocomplete="off" placeholder="请输入备注" class="limit" />
        </el-form-item>
      </el-form>
    </dtDialog>
  </div>
</template>

<script>
import api from '@/framework/api/role'
import { positiveInteger } from '@/framework/utils/validate'
export default {
  name: 'RoleAdd',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: undefined
    },
    title: {
      type: String,
      default: ''
    },
    closeForm: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      loading: false,
      // visible: false,
      subDisabled: false,
      roleTypeList: [],
      rules: {
        roleTypeCode: [{ required: true, message: '请选择角色类型', trigger: 'change' }],
        roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' },
          { pattern: /^[^#@%&*¥$*]+$/, message: '不要输入特殊字符' }
        ],
        roleCode: [{ required: true, message: '请输入角色编码', trigger: 'blur' },
          { pattern: /^[A-Za-z0-9]+$/, message: '请输入字母或数字' }
        ],
        roleSort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
        remark: [{ required: false, pattern: /^[^#@%&*¥$*]+$/, message: '不要输入特殊字符' }]
      }
    }
  },
  created() {
    this.getRoleType()
  },
  methods: {
    // 校验排序规则
    initPositiveInteger(data) {
      return positiveInteger(data)
    },
    // 关闭对话框触发
    handleClose() {
      // 清空数据关闭页面
      this.$refs['form'].resetFields()
      this.closeForm()
    },
    // 提交
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.submit()
        } else {
          return false
        }
      })
    },
    async submit() {
      let res = null
      this.subDisabled = true
      try {
        if (this.formData.roleId) {
          this.formData.dataScopeType = null
          res = await api.updateRole(this.formData)
        } else {
          res = await api.addRole(this.formData)
        }
        if (res.code === '00000') {
          this.$message({
            type: 'success',
            message: this.title == '新增角色' ? '新增成功' : '修改成功'
          })
          this.handleClose()
        } else {
          this.$message({
            type: 'error',
            message: this.title == '新增角色' ? '新增失败' : '修改失败'
          })
        }
        this.loading = false
        this.subDisabled = false
      } catch (error) {
        this.loading = false
        this.subDisabled = false
      }
    },
    // 获取角色类型列表
    async getRoleType() {
      var params = {
        'dictTypeCode': 'role_type'
      }
      const res = await api.getDictList(params)
      this.roleTypeList = res.data
    }
  }
}
</script>

<style scoped>

</style>
